package au.com.handsunited.web.command.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.language.Language;
import au.com.handsunited.model.servicecategory.ServiceCategory;
import au.com.handsunited.model.skill.Skill;

/**
 * @author arunc
 *
 */
public abstract class AbstractBaseEntityCommand {


	protected BaseEntity entity;

	protected List<String> serviceCategoryIds;
	protected Map<Long, ServiceCategory> serviceCategoryMap;

	protected List<String> languageIds;
	protected Map<Long, Language> languageMap;

	protected List<String> skillIds;
	protected Map<Long, Skill> skillMap;

	private static Logger logger = LoggerFactory.getLogger(AbstractBaseEntityCommand.class);


	public AbstractBaseEntityCommand() {
		super();
		serviceCategoryMap = new TreeMap<Long, ServiceCategory>();
		serviceCategoryIds = new ArrayList<String>();
		languageMap = new TreeMap<Long, Language>();
		languageIds = new ArrayList<String>();
		skillMap = new TreeMap<Long, Skill>();
		skillIds = new ArrayList<String>();
	}


	/**
	 * @param entity
	 */
	public AbstractBaseEntityCommand(BaseEntity entity) {
		this();
		this.entity = entity;
	}


	/**
	 * @return the serviceCategoryIds
	 */
	public List<String> getServiceCategoryIds() {
		if (serviceCategoryIds == null) {
			serviceCategoryIds = new ArrayList<String>();
		}

		if (serviceCategoryIds.size() == 0) {
			for (ServiceCategory serviceCategory : entity.getServiceCategories()) {
				serviceCategoryIds.add(serviceCategory.getId().toString());
			}
		}
		return serviceCategoryIds;
	}

	/**
	 * @param serviceCategoryIds the serviceCategoryIds to set
	 */
	public void setServiceCategoryIds(List<String> serviceCategoryIds) {

		if (entity.getServiceCategories() == null) {
			entity.setServiceCategories(new TreeSet<ServiceCategory>());
		}

		entity.getServiceCategories().clear();

		if (serviceCategoryIds == null) {
			this.serviceCategoryIds = new ArrayList<String>();
		} else {
			this.serviceCategoryIds = serviceCategoryIds;
			for (String serviceCategoryId : serviceCategoryIds) {
				ServiceCategory serviceCategory = serviceCategoryMap.get(Long.parseLong(serviceCategoryId));
				entity.getServiceCategories().add(serviceCategory);
			}
		}

	}

	/**
	 * @return the serviceCategoryMap
	 */
	public Map<Long, ServiceCategory> getServiceCategoryMap() {
		return serviceCategoryMap;
	}

	/**
	 * @param serviceCategoryMap the serviceCategoryMap to set
	 */
	public void setServiceCategoryMap(
			Map<Long, ServiceCategory> serviceCategoryMap) {
		this.serviceCategoryMap = serviceCategoryMap;
	}


	/**
	 * @return the languageIds
	 */
	public List<String> getLanguageIds() {
		if (languageIds == null) {
			languageIds = new ArrayList<String>();
		}
		if (languageIds.size() == 0 && entity != null) {
			for (Language language : entity.getLanguages()) {
				languageIds.add(language.getId().toString());
			}
		}

		return languageIds;
	}

	/**
	 * @param languageIds the languageIds to set
	 */
	public void setLanguageIds(List<String> languageIds) {

		if (entity.getLanguages() == null) {
			entity.setLanguages(new TreeSet<Language>());
		}

		entity.getLanguages().clear();

		if (languageIds == null) {
			this.languageIds = new ArrayList<String>();
		} else {
			this.languageIds = languageIds;

			for (String languageId : languageIds) {
				Language language = languageMap.get(Long.parseLong(languageId));
				entity.getLanguages().add(language);
			}
		}

	}

	/**
	 * @return the languageMap
	 */
	public Map<Long, Language> getLanguageMap() {
		return languageMap;
	}


	/**
	 * @param languageMap the languageMap to set
	 */
	public void setLanguageMap(Map<Long, Language> languageMap) {
		this.languageMap = languageMap;
	}



	/**
	 * @return the skillIds
	 */
	public List<String> getSkillIds() {
		if (skillIds == null) {
			skillIds = new ArrayList<String>();
		}

		if (skillIds.size() == 0) {
			for (Skill skill : entity.getSkills()) {
				skillIds.add(skill.getId().toString());
			}
		}

		return skillIds;
	}

	/**
	 * @param skillIds the skillIds to set
	 */
	public void setSkillIds(List<String> skillIds) {

		if (entity.getSkills() == null) {
			entity.setSkills(new TreeSet<Skill>());
		}

		entity.getSkills().clear();

		if (skillIds == null) {
			this.skillIds = new ArrayList<String>();
		} else {
			this.skillIds = skillIds;

			for (String skillId : skillIds) {
				Skill skill = skillMap.get(Long.parseLong(skillId));
				entity.getSkills().add(skill);
			}
		}

	}


	/**
	 * @return the skillMap
	 */
	public Map<Long, Skill> getSkillMap() {
		return skillMap;
	}


	/**
	 * @param skillMap the skillMap to set
	 */
	public void setSkillMap(Map<Long, Skill> skillMap) {
		this.skillMap = skillMap;
	}

}
